package com.maple.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.maple.admin.common.util.AjaxJson;
import com.maple.admin.domain.entity.Apply;
import com.maple.admin.domain.entity.Order;
import com.maple.admin.service.ApplyService;
import com.maple.admin.service.DetailService;
import com.maple.admin.service.OrderService;
import io.swagger.annotations.Api;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/order")
@Api(value = "order", tags = "订单模块")
public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private DetailService detailService;

    @Resource
    private ApplyService applyService;

    @GetMapping("/list")
    public AjaxJson selectAll(){

        return  AjaxJson.getSuccessData(orderService.list());
    }

    @GetMapping("/detail/{oid}")
    public AjaxJson getOrderDetail(@PathVariable Integer oid){

        return  AjaxJson.getSuccessData(detailService.getDetailByOid(oid));
    }

    @GetMapping("/{oid}")
    public AjaxJson getOrderD(@PathVariable Integer oid){

        return  AjaxJson.getSuccessData(orderService.getById(oid));
    }

    @PostMapping("/save")
    public AjaxJson saveOrder(@RequestBody Order order){

        return  AjaxJson.getSuccessData(orderService.saveOrUpdate(order));
    }

    @GetMapping("/delete/{oid}")
    @Transactional
    public AjaxJson delete(@PathVariable Integer oid){

        QueryWrapper wrapper  = new QueryWrapper<>();
        wrapper.eq("oid",oid);
        applyService.remove(wrapper);
        detailService.remove(wrapper);

        return AjaxJson.getSuccessData(orderService.removeById(oid));
    }
}
